package com.zhuoshi.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import redis.clients.jedis.Jedis;

import com.zhuoshi.bean.CreateRoomUtilModel;
import com.zhuoshi.bean.Wd_ht_buildModel;
import com.zhuoshi.bean.Wd_ht_projectModel;
import com.zhuoshi.bean.Wd_ht_roomModel;
import com.zhuoshi.service.Wd_ht_buildModelService;
import com.zhuoshi.service.Wd_ht_projectModelService;

@Controller
@RequestMapping("/build")
public class Wd_ht_buildAction {
	@Autowired
	Wd_ht_projectModelService wd_ht_projectModelService ;
	
	@Autowired
	Wd_ht_buildModelService wd_ht_buildModelService ;
    @RequestMapping("/selectAll.do")
   public String selectAll(Wd_ht_buildModel record,Model model){
		List<Wd_ht_buildModel> wd_ht_buildList = wd_ht_buildModelService.selectAll(record);
	    model.addAttribute("wd_ht_buildList", wd_ht_buildList);
		return "Wd_ht_build" ;
   }
	
	@RequestMapping("/Wd_ht_build_toAdd.do")
	public String wd_ht_build_toAdd(Model model){	
		List<Wd_ht_projectModel> wd_ht_projectList = wd_ht_projectModelService.selectAll(null);
		model.addAttribute("wd_ht_projectList", wd_ht_projectList);		
		return "Wd_ht_build_add" ;
	}
	
	@RequestMapping("/Wd_ht_build_add.do")
	public String wd_ht_build_add(Wd_ht_buildModel record,Model model){	
		wd_ht_buildModelService.insertSelective(record);
		return "redirect:/build/selectAll.do" ;
	}
	
	@RequestMapping("/Wd_ht_build_toUpdate.do")
	public String wd_ht_build_toUpdate(Wd_ht_buildModel record,Model model){	
		Wd_ht_buildModel wd_ht_buildBean = wd_ht_buildModelService.selectByPrimaryKey(record.getBuildId());
		model.addAttribute("bean", wd_ht_buildBean);
		return "Wd_ht_build_update" ;
	}
	
	@RequestMapping("/Wd_ht_build_update.do")
	public String wd_ht_build_update(Wd_ht_buildModel record,Model model){	
		wd_ht_buildModelService.updateByPrimaryKeySelective(record);
		return "redirect:/build/selectAll.do" ;
	}
	
	@RequestMapping("/Wd_ht_build_delete.do")
	public String wd_ht_build_delete(String buildId){	
		wd_ht_buildModelService.deleteByPrimaryKey(buildId);
		return "redirect:/build/selectAll.do" ;
	}
	
	@RequestMapping(value = "/addRoomredis.do")
	public @ResponseBody Map<String, Object> addRoomredis(@RequestBody CreateRoomUtilModel[] arr) {
		//Jedis jedis = RedisUtil.getJedis();
		Map<String, Object> map1 = new HashMap<String, Object>();
		Jedis jedis = new Jedis("6379");
       long time1 = System.currentTimeMillis();		
		for(CreateRoomUtilModel cru:arr){
			Map<String, String> map= new HashMap<String, String>();
		    UUID uuid = UUID.randomUUID();
	    	map.put("huxing", cru.getHuxing());
	    	map.put("number",cru.getNumber());
	    	map.put("lid", cru.getLid());
		    //map.put("", room.get);
	        jedis.hmset(uuid+"",map);
		}
		 long time2 = System.currentTimeMillis();	
	
		 System.out.println(time1+"-"+time2+"="+(time1-time2));
		 
        //RedisUtil.returnResource(jedis);
	
		System.out.println("添加成功");
		map1.put("msg", "成功");
		return map1;
	}
	
	@InitBinder
	public void InitBinder(ServletRequestDataBinder binder){
		binder.registerCustomEditor(Date.class, 
				new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
		
	}
	
	
}
